﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DARE;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;


namespace shaders
{
    /*/// <summary>
    /// This class defines a plane renderable as an AModel.
    /// It uses a CBasicEffect.
    /// The plane created has as default normal {0, 0, 1} so you have to orientate this one if you want another orientation.
    /// </summary>
    public class CPlane: AModel
    {
        #region fields

        protected Texture2D m_texture;
        protected string m_technique;
        protected Vector4 vLightPosition;
        protected Vector3 m_scale;
        protected VertexBuffer m_vertexBuffer;
        protected IndexBuffer m_indexBuffer;
        protected Type m_verticeType;

        #endregion

        #region properties
        
        public Vector2 Dimensions
        {
            get { return new Vector2(m_scale.X, m_scale.Y); }
            set { m_scale = new Vector3(value, 1); CreateModel(); }
        }

        public Vector4 LightPosition { get { return vLightPosition; } set { vLightPosition = value; } }

        public string Technique { get { return m_technique; } set { m_technique = value; } }

        #endregion

        #region ctor

        private void ctor(Texture2D texture, Vector2 dimensions, Type verticeType)
        {
            m_texture = texture;
            m_scale = new Vector3(dimensions, 1);
            m_effect = new CBasicEffect();
            m_technique = "SpecularLight";
            CreateModel();
            m_effect.ColorMap = m_texture;
        }

        public CPlane(string textureAssetName, Vector2 dimensions, Type verticeType)
        {
            ctor(CResourceMgr.Load<Texture2D>(textureAssetName), dimensions, verticeType);
        }

        public CPlane(Texture2D texture, Vector2 dimensions, Type verticeType)
        {
            ctor(texture, dimensions, verticeType);
        }

        public CPlane(Color color, Vector2 dimensions, Type verticeType)
        {
            Texture2D texture = new Texture2D(CDare.Instance.GraphicsDevice, 1, 1);
            Color[] c = new Color[1] { color };
            texture.SetData(c);
            ctor(texture, dimensions, verticeType);
        }

        #endregion

        #region methods

        /// <summary>
        /// When this method is called, it creates a vertical plane from (0, 0, 0) to (1, 1, 0).
        /// </summary>
        public void CreateModel()
        {
            
            VertexPositionNormalTextureTangentBinormal[] vertices = new VertexPositionNormalTextureTangentBinormal[4]
                {
                    new VertexPositionNormalTextureTangentBinormal(new Vector3(0f, 0f, 0f), new Vector3(0, 0, 1f), new Vector2(0, 0), new Vector3(1, 0, 0.0f), Vector3.UnitY),
                    new VertexPositionNormalTextureTangentBinormal(new Vector3(1f, 0f, 0f), new Vector3(0, 0, 1f), new Vector2(1, 0), new Vector3(1, 0, 0.0f), Vector3.UnitY),
                    new VertexPositionNormalTextureTangentBinormal(new Vector3(1f, -1f, 0f), new Vector3(0, 0, 1f), new Vector2(1, 1), new Vector3(1, 0, 0.0f), Vector3.UnitY),
                    new VertexPositionNormalTextureTangentBinormal(new Vector3(0f, -1f, 0f), new Vector3(0, 0, 1f), new Vector2(0, 1), new Vector3(1, 0, 0.0f), Vector3.UnitY)
                };
            m_vertexBuffer = new VertexBuffer(CDare.Instance.GraphicsDevice, typeof(VertexPositionNormalTextureTangentBinormal), vertices.Length, BufferUsage.None);

            /*VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[4]
                {
                    new VertexPositionNormalTexture(new Vector3(0f, 0f, 0f), new Vector3(0, 0, 1), new Vector2(0, 0)),
                    new VertexPositionNormalTexture(new Vector3(1f, 0f, 0f), new Vector3(0, 0, 1), new Vector2(1, 0)),
                    new VertexPositionNormalTexture(new Vector3(1f, -1f, 0f), new Vector3(0, 0, 1), new Vector2(1, 1)),
                    new VertexPositionNormalTexture(new Vector3(0f, -1f, 0f), new Vector3(0, 0, 1), new Vector2(0, 1))
                };
            m_vertexBuffer = new VertexBuffer(CDare.Instance.GraphicsDevice, typeof(VertexPositionNormalTexture), vertices.Length, BufferUsage.None);*/
    /*
            m_vertexBuffer.SetData<VertexPositionNormalTextureTangentBinormal>(vertices);

            short[] indices = new short[6] { 0, 1, 2, 0, 2, 3 };
            m_indexBuffer = new IndexBuffer(CDare.Instance.GraphicsDevice, IndexElementSize.SixteenBits, indices.Length, BufferUsage.None);
            m_indexBuffer.SetData<short>(indices);

        }

        public override void Update(GameTime gameTime)
        {
            
        }

        public override void Draw(GameTime gameTime)
        {
            if (Node.Render3D == null)
                return;
            if (Node.Render3D.CameraMgr.ActiveCamera == null)
                return;

            m_effect.CurrentTechnique = m_effect.Techniques[m_technique];
            m_effect.View = m_node.Render3D.CameraMgr.ActiveCamera.View;
            m_effect.Projection = m_node.Render3D.CameraMgr.ActiveCamera.Projection;
            m_effect.LightPosition = vLightPosition;
            m_effect.ColorMap = m_texture;
            m_effect.EyePosition = m_node.Render3D.CameraMgr.ActiveCamera.Position;
            foreach (EffectPass pass in m_effect.CurrentTechnique.Passes)
            {
                m_effect.World = Matrix.CreateScale(m_scale * m_node.NScale) * m_node.WorldMatrix;
                Matrix worldInverse = Matrix.Invert(Matrix.Transpose(m_effect.World));
                m_effect.WorldInverse = worldInverse; 
                pass.Apply();
                CDare.Instance.GraphicsDevice.SetVertexBuffer(m_vertexBuffer);
                CDare.Instance.GraphicsDevice.Indices = m_indexBuffer;
                CDare.Instance.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12);
            }
        }

        #endregion
    }*/
}
